package com.magicborrow.views;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.magicborrow.R;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.utils.ShareConfig;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class ShareDialog extends Dialog implements View.OnClickListener {

    private Context mContext;
    private HashMap<String, Object> map;
    private StuffBean stuffBean;

    public ShareDialog(Context context) {
        this(context, 0);
        this.mContext=context;
    }

    public ShareDialog(Context context, int themeResId) {
        super(context, R.style.share_Dialog);
        setContentView(R.layout.dialog_share);
        setDialogSize(1f);
        findViewById(R.id.tv_share_cabcel).setOnClickListener(this);
        map = new HashMap<>();
        findViewById(R.id.ll_sina).setOnClickListener(this);
        findViewById(R.id.ll_wechat).setOnClickListener(this);
        findViewById(R.id.ll_wechat_friend).setOnClickListener(this);
        findViewById(R.id.ll_qzone).setOnClickListener(this);
    }




    public void setDialogSize(double percent){
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mystyle);
        Display play = window.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        play.getMetrics(metrics);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=(int) ((metrics.widthPixels)*percent);
        params.height=params.WRAP_CONTENT;
        onWindowAttributesChanged(params);
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
        if (map.size() != 0) {
            map.clear();
        }
        switch (v.getId()){
            case R.id.tv_share_cabcel:
                this.dismiss();
                break;
            case R.id.ll_sina:
                share_SinaWeibo();
                break;
            case R.id.ll_wechat:
                share_WxFriend();
                break;
            case R.id.ll_wechat_friend:
                share_CircleFriend();
                break;
            case R.id.ll_qzone:
                share_Qzone();
                break;
        }
    }

    /**
     * 分享到新浪微博
     */
    private void share_SinaWeibo() {
        map.put("AppKey", ShareConfig.APPKEY_SINA_WEIBO);
        map.put("AppSecret", ShareConfig.APPSECRET_SINA_WEIBO);
        map.put("RedirectUrl", ShareConfig.REDIRECTURL_SINA_WEIBO);
        map.put("ShareByAppClient", ShareConfig.SHAREBYAPPCLIENT_SINA_WEIBO);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_SINA_WEIBO);
        map.put("Enable", ShareConfig.ENABLE_SINA_WEIBO);

        ShareSDK.setPlatformDevInfo(SinaWeibo.NAME, map);
        ShareSDK.initSDK(mContext, ShareConfig.APPKEY);
        cn.sharesdk.sina.weibo.SinaWeibo.ShareParams sp = new cn.sharesdk.sina.weibo.SinaWeibo.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setText(stuffBean.getName()+"\\n"+share_text+share_url);//分享的连接添加到文本中
        Platform weibo = ShareSDK.getPlatform(mContext, SinaWeibo.NAME);
        weibo.share(sp);
    }

    String share_text="借到意想不到的好物，分享别具一格的东西。";
    String share_url="http://www.mojoy.cc/api/ware/share?id=";

    // 绕过审核
    // 微信好友  文字或单张图片或者文件进行分享
    // 微信朋友圈  单张图片或者图片和文字一起分享
    /**
     * 分享到微信好友
     */
    private void share_WxFriend() {
        Platform circle = ShareSDK.getPlatform(mContext, Wechat.NAME);
        map.put("AppId", ShareConfig.APPID_WXFRIEND);
        map.put("Enable", ShareConfig.ENABLE_WXFRIEND);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_WXFRIEND);
        ShareSDK.setPlatformDevInfo(Wechat.NAME, map);
        ShareSDK.initSDK(mContext, ShareConfig.APPKEY);
        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(stuffBean.getName());
        sp.setText(share_text);
        sp.setUrl(share_url+stuffBean.getId());
        circle.share(sp); // 执行图文分享
    }

    /**
     * 分享到朋友圈
     */
    private void share_CircleFriend() {
        Platform circle = ShareSDK.getPlatform(mContext, WechatMoments.NAME);
        map.put("AppId", ShareConfig.APPID_CIRCLE_FRIEND);
        map.put("AppSecret", ShareConfig.APPSECRET_CIRCLE_FRIEND);
        map.put("Enable", ShareConfig.ENABLE_CIRCLE_FRIEND);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_CIRCLE_FRIEND);

        ShareSDK.setPlatformDevInfo(WechatMoments.NAME, map);
        ShareSDK.initSDK(mContext, ShareConfig.APPKEY);

        cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new cn.sharesdk.wechat.moments.WechatMoments.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(stuffBean.getName());
        //朋友圈分享的文本会挂掉
        sp.setText(share_text);
        sp.setUrl(share_url);
        circle.share(sp);
    }

    private void share_Qzone() { //分享到qq空间
        Platform qzone = ShareSDK.getPlatform(QZone.NAME);

        map.put("AppId", ShareConfig.APPID_QZONE);
        map.put("AppKey", ShareConfig.APPKEY_QZONE);
        map.put("ShareByAppClient", ShareConfig.SHAREBYAPPCLIENT_QZONE);
        map.put("Enable", ShareConfig.ENABLE_QZONE);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_QZONE);

        ShareSDK.setPlatformDevInfo(QZone.NAME, map);
        ShareSDK.initSDK(getContext(), ShareConfig.APPKEY);

        cn.sharesdk.tencent.qzone.QZone.ShareParams sp = new cn.sharesdk.tencent.qzone.QZone.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(stuffBean.getName());
        sp.setText(share_text);
        sp.setTitleUrl(share_url);//标题的超链接
        sp.setSite("魔借");
        sp.setSiteUrl(share_url);

        qzone.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                System.out.println("onComplete");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                System.out.println("onError");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                System.out.println("onCancel");
            }
        });
        qzone.share(sp);
    }

    public void setData(StuffBean stuff) {
        this.stuffBean=stuff;
    }
}
